{% extends 'layout/base.html' %}
{% from 'layout/macros.html' import render_pagination with context %}

{% block content %}
<div class="row mt-5">
    <div class="col-lg"></div>
    <div class="col-lg-10">
        {% set text = 'Zones' %}
        {% if zones.results.total > 0 %}
            {% set text = text + ' - ' + '{0:,}'.format(zones.results.total) + ' result' %}
            {% if zones.results.total > 1 %}
                {% set text = text + 's' %}
            {% endif %}
        {% endif %}
        <h3 class="mb-4">{{ text }}</h3>

        <div class="mb-2">
            <div class="row">
                <div class="col">
                    <a href="{{ url_for('dns.zone_edit', dns_zone_id=0) }}" class="btn btn-primary"> <i class="fas fa-plus"></i> <span class="ml-1">new zone</span></a>
                </div>

                <div class="col-7 mx-0 px-0">
                    <form method="get" action="{{ url_for('dns.index') }}">
                        <div class="row">
                            <div class="col px-0 mx-0">
                                {% if tags|length > 0 %}
                                <div class="form-group">
                                    <select name="tags" id="tags" class="form-control custom-select" multiple="multiple" data-placeholder="tags">
                                        {% for tag in tags %}
                                        <option value="{{ tag.name }}" {{ 'selected' if tag.name in search_tags }}>{{ tag.name }}</option>
                                        {% endfor %}
                                    </select>
                                </div>
                                {% endif %}
                            </div>
                            <div class="col-5 px-0 mx-2">
                                <div class="form-group">
                                    <div class="input-group">
                                        <input type="text" name="search" id="search" class="form-control" placeholder="search" value="{{ search }}">
                                        <div class="input-group-append">
                                            <button type="submit" class="btn btn-sm btn-primary"><i class="fas fa-search"></i></button>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </form>
                </div>

                <div class="col-1 mr-2">
                    <a class="btn btn-secondary dropdown-toggle" href="#" role="button" id="dropdownMenuLink" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
                        <i class="fas fa-ellipsis-h"></i>
                    </a>
                    <div class="dropdown-menu" aria-labelledby="dropdownMenuLink">
                        <a href="{{ url_for('dns.zones_import') }}" class="dropdown-item"> <i class="fas fa-file-import"></i> <span class="ml-1">import</span></a>
                        <form action="{{ url_for('dns.zones_export') }}" method="post">
                            <input type="hidden" name="csrf_token" value="{{ csrf_token() }}">
                            <input type="hidden" name="tags" value="{{ ','.join(search_tags) }}">
                            <input type="hidden" name="search" value="{{ search }}">
                            <a href="#" class="submit-on-click dropdown-item"> <i class="fas fa-file-export"></i> <span class="ml-1">export</span></a>
                        </form>

                        <div class="dropdown-divider"></div>

                        <form action="{{ url_for('dns.zone_group_delete') }}" method="post" id="delete-group-zones">
                            <input type="hidden" name="csrf_token" value="{{ csrf_token() }}">
                            <input type="hidden" name="tags" value="{{ ','.join(search_tags) }}">
                            <input type="hidden" name="search" value="{{ search }}">
                            <a href="#" class="confirm-delete dropdown-item text-danger" data-text="Are you sure you want to delete all the domains displayed? Depending on the number of results, this action may take a long time to complete."> <i class="fas fa-trash"></i> <span class="ml-1">delete</span></a>
                        </form>
                    </div>
                </div>
            </div>
        </div>

        {% if zones.results.total == 0 and search_tags|length == 0 and search|length == 0 %}
        <div class="alert alert-info text-center">No zones configured yet.</div>
        {% else %}
        <table class="table table-striped table-responsive-lg">
            <thead>
            <tr>
                <th>#</th>
                <th style="width: 100%;">Domain</th>
                {% if current_user.admin %}
                <th>User</th>
                {% endif %}
                <th class="text-center">Active</th>
                <th class="text-center" style="white-space: nowrap;">Catch-All</th>
                <th class="text-center">Records</th>
                <th class="text-center">Matches</th>
                <th class="text-center">Forwarding</th>
                <th class="text-center">Regex</th>
                <th></th>
            </tr>
            </thead>
            <tbody>
            {% for zone in zones.zones %}
            <tr>
                <td>{{ loop.index + (per_page * (page - 1)) }}</td>
                <td>
                    <div>
                        <a href="{{ url_for('dns.zone_view', dns_zone_id=zone.id) }}" class="text-info">{{ zone.domain }}</a>
                    </div>
                    {% if zone.tags|length > 0 %}
                    <div class="mt-1" title="tags">
                        {% for tag in zone.tags %}
                        <span class="badge badge-secondary">{{ tag }}</span>
                        {% endfor %}
                    </div>
                    {% endif %}
                </td>
                {% if current_user.admin %}
                <td>{{ zone.username }}</td>
                {% endif %}
                <td class="text-center">
                    {% set class = 'success' if zone.active else 'danger' %}
                    {% set icon = 'check' if zone.active else 'times' %}
                    <span class="text-{{ class }}"><i class="fas fa-{{ icon }} fa-fw"></i></span>
                </td>
                <td class="text-center">
                    {% set class = 'success' if zone.catch_all else 'danger' %}
                    {% set icon = 'check' if zone.catch_all else 'times' %}
                    <span class="text-{{ class }}"><i class="fas fa-{{ icon }} fa-fw"></i></span>
                </td>
                <td class="text-center">{{ zone.record_count }}</td>
                <td class="text-center">{{ zone.match_count }}</td>
                <td class="text-center">
                    {% set class = 'success' if zone.forwarding else 'danger' %}
                    {% set icon = 'check' if zone.forwarding else 'times' %}
                    <span class="text-{{ class }}"><i class="fas fa-{{ icon }} fa-fw"></i></span>
                </td>
                <td class="text-center">
                    {% set class = 'success' if zone.regex else 'danger' %}
                    {% set icon = 'check' if zone.regex else 'times' %}
                    <span class="text-{{ class }}"><i class="fas fa-{{ icon }} fa-fw"></i></span>
                </td>
                <td class="text-right" style="white-space: nowrap;">
                    <a href="{{ url_for('dns.zone_edit', dns_zone_id=zone.id) }}" title="edit zone"><i class="fas fa-edit"></i></a>
                    <a href="{{ url_for('logs.index', domain='%' ~ zone.domain, user_id=zone.user_id) }}" class="ml-1" title="view logs"><i class="fas fa-clipboard-list"></i></a>
                    {% if zone.master == False %}
                    <form action="{{ url_for('dns.zone_delete', dns_zone_id=zone.id) }}" method="post" class="d-inline ml-1" id="delete-zone-form-{{ zone.id }}">
                        <input type="hidden" name="csrf_token" value="{{ csrf_token() }}">
                        <a href="#" class="confirm-delete text-danger" data-text="Are you sure you want to delete this domain?" title="delete zone"><i class="fas fa-trash"></i></a>
                    </form>
                    {% endif %}
                </td>
            </tr>
            {% endfor %}
            </tbody>
        </table>

        {% if zones.results.pages > 1 %}
            {{ render_pagination(zones.results, url_for('dns.index'), page_url) }}
        {% endif %}
        {% endif %}
    </div>
    <div class="col-lg"></div>
</div>

<script type="text/javascript">
    $(document).ready(function() {
        $('#tags').select2({
            tags: true
        });
    });
</script>
{% endblock %}